import React from 'react';
import { withRouter } from 'react-router-dom';
import { inject, observer } from 'mobx-react';
import { Tools, Bubble } from 'weaver-mobile';
import { BrowserHrm } from 'weaver-mobile-page';
import { getLabel } from "../../util/common";
import { addshare } from './../../apis/form';

@inject('wfGlobalStore')
@withRouter
@observer
export default class Main extends React.Component {

    render() {
        const { variableMap } = this.props.wfGlobalStore;
        const { requestid } = this.props.wfGlobalStore.commonParam;
        const defaultTabs = [
            { name: getLabel(31786, '流程参与人'), key: '8' },
            { name: getLabel(24515, '最近'), key: '1' },
            { name: getLabel(18511, '同部门'), key: '2' },
            { name: getLabel(15089, '我的下属'), key: '3' },
            { name: getLabel(16455, '组织结构'), key: '4' },
            { name: getLabel(81554, '常用组'), key: '5' },
            { name: getLabel(1340, '所有人'), key: '6' },
        ]
        return (
            <BrowserHrm
                label="人力多选"
                store={variableMap.get('dingStore')}
                hasAdvanceSerach
                type={1}
                isSingle={false}
                hasDing
                dingUrl={"/api/workflow/forward/getReqWfNodeOperators"}
                dingParams={{
                    requestid,
                    isFromEMBrow: 1,
                    isDistinct: 0
                }}
                valueArray={[]}
                onConfirm={this.creatDing}
                tabs={defaultTabs}
                // notGoBack={true}
            />
        )
    }

    //发消息给钉钉

    creatDing = (ids, names, objArr) => {
        const { requestid, f_weaver_belongto_userid,f_weaver_belongto_usertype } = this.props.wfGlobalStore.commonParam;
        const { firstSharer } = this.props.wfGlobalStore.urlParams;
        const { requestname } = this.props.wfGlobalStore.invalidParam;
        let sharer_new = firstSharer ? firstSharer : f_weaver_belongto_userid;
        let origin = window.location.origin;
        if (ids === "") {
            Bubble.warning(getLabel(131696,"请选择接收人"))
            return; 
        };
        if (Tools.ding && Tools.ding.createDing) {
            Tools.ding.createDing(
                {
                    users: ids.split(","), //默认选中用户工号列表；类型: Array<String>
                    alertType: 2, // 钉发送方式 0:电话, 1:短信, 2：应用内；类型 Number
                    alertDate: { "format": "yyyy-MM-dd HH:mm", "value": "2019-08-29 08:25" }, // 非必选，定时发送时间, 非定时DING不需要填写
                    type: 2,// 附件类型 1：image, 2：link；类型: Number
                    // 非必选
                    // 附件信息
                    attachment: {
                        image: origin + "/images/ecology9/workflow/link_img.png", // 链接附件, type=2时, 必选；类型: String    
                        title: requestname, // 链接附件, type=2时, 必选；类型: String
                        url: origin + "/common/chatResource/view.html?resourcetype=0&resourceid=" + requestid + "&isfromchatshare=1&sharer=" + f_weaver_belongto_userid + 
                        "&firstSharer=" + sharer_new + "&em_client_type=DING" + "&isFormMsg=1" + "&f_weaver_belongto_userid="+f_weaver_belongto_userid+"&f_weaver_belongto_usertype="+f_weaver_belongto_usertype, // 链接附件, type=2时, 必选；类型 String
                        text: origin // 链接附件, type=2时, 必选；类型: String
                    },
                    text: '',  // 正文
                    bizType: 0, // 业务类型 0：通知DING；1：任务；2：会议；
                },
                successRes => {
                    let params = {
                        resourcetype: 0,
                        resourceid: requestid,
                        isfromchatshare: 1,
                        firstSharer: sharer_new,
                        resourceids: ids.split(","),
                        f_weaver_belongto_userid:f_weaver_belongto_userid,
                        f_weaver_belongto_usertype:f_weaver_belongto_usertype
                    }

                    addshare(params).then(res => {
                        console.log("addshare-res", res)
                    })
                },
                failError => {
                    console.log('failError', failError);
                }
            )
        }
    }

}




{/* <script>
jQuery(document).ready(function(){
WfForm.registerCheckEvent(WfForm.OPER_PRINTPREVIEW, function(callback){
        setTimeout(function(){    callback(); },2000)
    });
})
</script> */}
